use [family-annal]
GO
-- Drop trigger in table tb_fa_license
IF OBJECT_ID ('tb_fa_license.trigger_fa_license_insert_update','TR') IS NOT NULL
   DROP TRIGGER tb_fa_license.trigger_fa_license_insert_update;
GO
-- Drop table and all constraint if exists
IF  EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_license]') AND name = N'pk_tb_fa_license')
BEGIN
ALTER TABLE [dbo].[tb_fa_license] DROP CONSTRAINT [pk_tb_fa_license]
END
GO
USE [family-annal]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tb_fa_license]') AND type in (N'U'))
DROP TABLE [dbo].[tb_fa_license]
GO
use [family-annal]
GO
-- create table license
create table tb_fa_license
(
	license_id int not null,
	biography_id int,
	license_info nvarchar(max), -- use defined string json on c# source code
	license_file_code varchar(max),
	create_date datetime,
	create_uid int,
	update_date datetime,
	update_uid int,
	license_status varchar(1) default 'Y'
);
GO
use [family-annal];
GO
-- create trigger trigger_fa_license_insert_update
CREATE TRIGGER trigger_fa_license_insert_update
   ON  tb_fa_license 
   AFTER INSERT, UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for trigger here
    IF EXISTS(SELECT * FROM INSERTED)
    BEGIN
		update tb_fa_license set create_date = GETDATE()
		from tb_fa_license t join inserted i
		on t.license_id = i.license_id
    END
    IF NOT EXISTS(SELECT * FROM UPDATED)
    BEGIN
		update tb_fa_license set update_date = GETDATE()
		from tb_fa_license t join updated u
		on t.license_id = u.license_id;
    END
END
GO